﻿namespace NJQuery {
	using System;
	using System.Web.UI;
	using System.Web.UI.WebControls;
	using System.Collections.Generic;
	using System.ComponentModel;

	/// <summary>
	/// Server control used to generate a <see href="http://jqueryui.com/themeroller/">Query UI Themed Symbol</see>.
	/// </summary>
	public class ThemedIcon : ManagedWebControl {


		#region Static Readonly Fields: Property defaults

		private static readonly Icon defaultIcon = Icon.None;

		#endregion


		#region Constructors

		/// <summary>
		/// Initializes a new instance of the <see cref="ThemedIcon"/> class.
		/// </summary>
		public ThemedIcon() : base(HtmlTextWriterTag.Span) { }

		#endregion


		#region Properties

		/// <summary>
		/// Gets or sets the standard jQuery UI themed symbol to render.
		/// </summary>
		[DefaultValue(typeof(Icon), null), Category("Appearance")]
		public virtual Icon Icon {
			get { return ViewState["Icon"] as Icon? ?? defaultIcon; }
			set { ViewState["Icon"] = value; }
		}

		#endregion


		#region Methods: Render hooks

		/// <summary>
		/// Allows a jQuery server control to reference scripts and stylesheets necessary to render their client markup and widgets.
		/// </summary>
		/// <param name="manager">The <see cref="Manager"/> used to handle style and script referencing in addition to unified generation of load and ready scripts.</param>
		protected override void Register(Manager manager) {
			manager.RegisterClientStyle(ClientStyle.UICore);
			manager.RegisterTheme();
		}

		/// <summary>
		/// Adds HTML attributes and styles that need to be rendered to the specified <see cref="HtmlTextWriter"/>.
		/// This method is used primarily by control developers.
		/// </summary>
		/// <param name="writer">A <see cref="HtmlTextWriter"/> that represents the output stream to render HTML content on the client.</param>
		protected override void AddAttributesToRender(HtmlTextWriter writer) {

			if(!MasterUseCssStyling) {
				writer.AddStyleAttribute(HtmlTextWriterStyle.Display, "inline");
				writer.AddStyleAttribute(HtmlTextWriterStyle.Width, "1px");
				writer.AddStyleAttribute(HtmlTextWriterStyle.PaddingRight, "15px");
				writer.AddStyleAttribute(HtmlTextWriterStyle.FontSize, "15px");
				writer.AddStyleAttribute("zoom", "1");
			}

			base.AddAttributesToRender(writer);
		}

		/// <summary>
		/// Sends server control content to a provided <see cref="HtmlTextWriter"/> object, which writes the content to be rendered to the client.
		/// This method is used primarily by control developers.
		/// </summary>
		/// <param name="writer">A <see cref="HtmlTextWriter"/> that represents the output stream to render HTML content on the client.</param>
		public override void RenderControl(HtmlTextWriter writer) {

			if(string.IsNullOrEmpty(Icon.CssClass)) return;

			base.RenderControl(writer);
		}

		/// <summary>
		/// Enumerates additional CSS classes to be added to the HTML tag.
		/// This method is used primarily by control developers.
		/// </summary>
		/// <returns>The additional CSS classes.</returns>
		protected override IEnumerable<string> GetAdditionalCssClasses() {
			yield return "ui-icon";
			yield return Icon.CssClass;
		}


		#endregion


	}

}